package com.danskin.wechat.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.danskin.wechat.dao.CustInfoExDao;
import com.danskin.wechat.dao.CustOrderDetailDao;
import com.danskin.wechat.dao.CustOrderPaymentDao;
import com.danskin.wechat.dao.CustOrderRecordDao;
import com.danskin.wechat.dao.CustPointRecordDao;
import com.danskin.wechat.dao.StoreInfoDao;
import com.danskin.wechat.entity.CustInfoEx;
import com.danskin.wechat.entity.CustInfoExView;
import com.danskin.wechat.entity.CustOrderDetail;
import com.danskin.wechat.entity.CustOrderPayment;
import com.danskin.wechat.entity.CustOrderRecord;
import com.danskin.wechat.entity.CustPointRecord;
import com.danskin.wechat.entity.StoreInfo;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.service.CustOrderRecordService;
import com.danskin.wechat.service.WechatMsgTemplateService;
import com.danskin.wechat.util.DateUtils;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;
import com.danskin.wechat.vo.CustOrderDetailVo;
import com.danskin.wechat.vo.CustOrderRecordVo;

@Service
public class CustOrderRecordServiceImpl implements CustOrderRecordService {
	@Autowired
	private CustOrderRecordDao custOrderRecordDao;
	@Autowired
	private CustOrderDetailDao custOrderDetailDao;
	@Autowired
	private CustOrderPaymentDao custOrderPaymentDao;
	@Autowired
	private CustInfoExDao custInfoExDao;
	@Autowired
	private StoreInfoDao storeInfoDao;

	@Autowired
	private WechatMsgTemplateService msgTemplateService;

	@Override
	public CustOrderRecord findCustOrderRecord(String appId, String openId, String orderNo) {
		// TODO Auto-generated method stub
		return custOrderRecordDao.findCustOrderRecord(appId, openId, orderNo);
	}

	public List<CustOrderDetail> queryCustOrderDetail(String orderNo) {
		// TODO Auto-generated method stub
		return custOrderRecordDao.queryCustOrderDetail(orderNo);
	}

	@Override
	public int saveCustOrderRecord(WxMediaPlatform mp, CustOrderRecord entity, List<CustOrderDetail> detailList,
			List<CustOrderPayment> paymentList) throws Exception {

		CustInfoExView custInfoEx = custInfoExDao.selectCustByMemberId(mp.getId(), entity.getMemberId());
		if (custInfoEx != null) {

			int insertCount = 0;

			entity.setCustExId(custInfoEx.getId());
			try {

				insertCount = custOrderRecordDao.insertSelective(entity);
				if (insertCount > 0) {
					int orderId = entity.getId();
					// 保存订单明细
					for (CustOrderDetail d : detailList) {
						// d.setCustExId(custInfoEx.getId());
						d.setOrderId(orderId);
					}
					if (detailList != null && detailList.size() > 0) {
						custOrderDetailDao.insertBatch(detailList);
					}
					// 保存订单支付信息
					// for (CustOrderPayment d : paymentList) {
					// d.setCustExId(custInfoEx.getId());
					// d.setOrderId(orderId);
					// }
					// custOrderPaymentDao.insertBatch(paymentList);

					// 发送支付成功模版消息
					// StoreInfo storeInfo =
					// storeInfoDao.selectByStoreCode(entity.getStoreCode());
					// String storeName = storeInfo != null ?
					// storeInfo.getName() : entity.getStoreCode();
					// msgTemplateService.sendCustPayOrderMsg(mp.getAccessToken(),
					// mp.getAppId(), custInfoEx.getWxOpenId(),
					// custInfoEx.getName(), entity.getOrderNo(),
					// entity.getPayAmount(), storeName,
					// entity.getTransTime());
				}
			} catch (Exception e) {
				LogUtil.error("========新增会员交易记录异常", e);
				throw e;
			}
			return insertCount;
		}
		return -1;
	}

	public Double selectPayAmountByYear(CustOrderRecord record) {
		Double amountByYear = custOrderRecordDao.selectPayAmountByYear(record);
		return amountByYear;
	}

	@Override
	public List<CustOrderRecord> listPageCustOrderRecordByOpenId(Page page) {

		return custOrderRecordDao.listPageByOpenId(page);
	}

	@Override
	public CustOrderRecord queryCustOrderRecord(int orderId, String openId) {

		return custOrderRecordDao.selectByIdAndOpenId(orderId, openId);
	}

	@Override
	public CustOrderRecordVo queryCustOrderRecordVo(int orderId, String openId) {
		CustOrderRecord record = this.queryCustOrderRecord(orderId, openId);
		if (record != null) {
			CustOrderRecordVo recordVo = new CustOrderRecordVo();
			recordVo.setOrderNo(record.getOrderNo());
			recordVo.setTransTime(DateUtils.formatTime(record.getTransTime()));
			recordVo.setOrderStatus("已完成");
			recordVo.setStoreCode(record.getStoreCode());
			recordVo.setSource(record.getSource());
			recordVo.setGetPoints(record.getGetPoints());
			recordVo.setTotalAmt(record.getTotalAmount());
			recordVo.setPayAmt(record.getPayAmount());
			recordVo.setDiscountAmt(record.getTotalAmount().subtract(record.getPayAmount()));

			List<CustOrderDetail> details = custOrderDetailDao.selectByOrderId(orderId);
			if (details != null && details.size() > 0) {
				List<CustOrderDetailVo> detailList = new ArrayList<CustOrderDetailVo>();
				for (CustOrderDetail d : details) {
					CustOrderDetailVo detailVo = new CustOrderDetailVo();
					detailVo.setProdName(d.getProductName());
					detailVo.setTotalPrice(d.getTotalPrice());
					detailVo.setSize(d.getSize());
					detailVo.setColorName(d.getColorName());
					detailVo.setQuantity(d.getQuantity());
					detailList.add(detailVo);
				}

				recordVo.setDetailList(detailList);
			}

			return recordVo;
		}
		return null;
	}
}
